/**
 * 我的资料
 * @param {Object} _in 
 */
function tomyProfile(_in){
	window.localStorage.setItem("_in",_in);
}

/**
 *  给我的资料赋值
 */
//function myProfileInfo(){
//	var user = JSON.parse($.cookie("user"));
//	if(user!=null && user!=""){
//		console.log( user.email);
//		$("#user_id").val(user.user_id);
//		$("#email").val(user.email);
//		$("#phone").val(user.mobile);
//		$("#userName").val(user.user_name);
//		$("#showName").val(user.show_name);
//		$("input[type=radio][name=sex][value='"+user.sex+"']").attr("checked",'checked');
//		$("select option[value='"+user.age+"']").attr("selected","selected");  //如果值一样 就选中对应的option,
//	}
//}
/**
 * 修改用户信息
 */
function updateUserInfo(){
	var nowPassword = $("#nowPassword").val();
	var newPassword = $("#newPassword").val();
	var confirmPwd = $("#confirmPwd").val();
	console.log(nowPassword+" ，"+newPassword+" ，"+confirmPwd)
	$.ajax({
		type:"post",
		url:"http://127.0.0.1:8080/inxweb/userInfo/upinfo",
		data:$("#updateForm").serialize(),
		//async:true
		success: function(data){
			if(data.success){
				alert("信息修改成功！");
				$.cookie("user",JSON.stringify(data.entity));
			}else{
				alert("信息修改失败！");
			}
		}
	});
}
/**
 * 修改密码
 */
function updatePwd(){
	var nowPassword = $("#nowPassword").val();
	var newPassword = $("#newPassword").val();
	var confirmPwd = $("#confirmPwd").val();
	var token = localStorage["token"];
	console.log(nowPassword+" ，"+newPassword+" ，"+confirmPwd)
	if(newPassword!=confirmPwd){
		alert("两次密码输入不一致！");
		return;
	}
	$.ajax({
		type:"post",
		url:"http://localhost:9095/user/update/password",
		data:$("#pwdForm").serialize(),
		headers:{
			"token":token
		},
		//async:true
		success: function(data){
			if(data.code==200){
				alert("密码修改成功！");
			}else{
				alert("原密码不正确");
			}
		}
	});
}
/**
 *  站内信
 */
function getSysInfo(){
	var user = JSON.parse($.cookie("user"));
	//var sysinfo = $("#sysinfo")
	if(user!=null && user!=""){
		$.ajax({
			type:"post",
			url:"http://127.0.0.1:8080/inxweb/userInfo/sysinfo",
			data:{"user_id":user.user_id},
			//async:true
			success: function(data){
				if(data.success){
					var html = '';
					if(data.entity!=null && data.entity!=""){
						$.each(data.entity, function(index,value) {
							html += "<div class='u-sys-news'>"+
								"<dl id='del3'> <dt> <section class='tar'> <p class='hLh30'><b class='fsize14 f-fA c-red'>系统消息</b> </p> <p class='hLh20'><span class='f-fA c-666'>"+value.add_time.substring(5,7)+"月"+value.add_time.substring(8,11)+"日  "+value.add_time.substring(11,16)+"</span></p> <p class='hLh20'><span class='f-fA c-999'>"+value.add_time.substring(0,4)+"年</span></p> </section> </dt>"+
									"<dd>"+
										"<section class='mt10'>"+
											"<div style='line-height: 200%;'>"+
												"<p><span class='c-666 f-fA'>"+value.content+"</span></p>"+
											"</div>"+
											"<div class='mt10 tar'>"+
												"<a href='javascript:void(0)' onclick='delULetter("+value.id+")' title='' class='comm-btn c-btn-6'>删 除</a>"+
											"</div>"+
										"</section>"+
									"</dd>"+
								"</dl>"+
							"</div>";
						});
						$("#sysinfo").append(html);
					}else{
						$("#nosysinfo").show();
					}
				}else{
					alert(data.message);
				}
			}
		});
	}
}
/**
 *  删除系统消息（站内信）
 */
function delULetter(id){
	$.ajax({
		type:"post",
		url:"http://127.0.0.1:8080/inxweb/userInfo/delinfo",
		data:{"id":id},
		//async:true
		success: function(data){
			if(data.success){
				alert("系统消息删除成功");
				window.location.reload();
			}else{
				alert("系统消息删除失败！");
			}
		}
	});
}
/**
 * 	我的收藏
 */
function getMyFavorites(){
	var user = JSON.parse($.cookie("user"));
	if(user!=null && user!=""){
		$.ajax({
			type:"post",
			url:"http://127.0.0.1:8080/inxweb/userInfo/favorites",
			data:{"id":user.user_id},
			success: function(data){
				if(data.success){
					var html = '';
					var time = '';
					if(data.entity!=null && data.entity!=""){
						$.each(data.entity, function(index,value) {
							time = getMyDate(value.add_time);
							html += "<dl> <dt> <section class='tar'> <p class='hLh30'> <b class='fsize14 f-fA c-red'>"+time.substring(0,4)+"年</b> </p> <p class='hLh20'> <span class='f-fA c-666'>"+time.substring(5,7)+"月"+time.substring(8,10)+"日  "+time.substring(11,16)+"</span> </p> <p class='hLh20'> <span class='f-fA c-999'>收藏</span> </p> </section> </dt>"+
										"<dd>"+
											"<section class='mt10'>"+
												"<div class='of cancel-colle'>"+
													"<div class='fr tac'> <label class='hand'><input type='checkbox' style='vertical-align: -2px;' name='favouriteId' value='"+value.id+"'></label> <br>"+
														"<a href='javascript:void(0)' onclick='delMyFavorites("+value.id+")' class='c-blue'>取消收藏</a>"+
													"</div>"+
													"<a href='http://localhost:8081/front/couinfo/15' title=''> <img src='http://localhost:8081"+value.logo+"' width='120' alt=''> </a>"+
												"</div>"+
												"<div class='hLh30 txtOf'>"+
													"<a href='http://localhost:8081/front/couinfo/15' class='c-666 fsize14'>"+value.course_name+"</a>"+
												"</div>"+
											"</section>"+
										"</dd>"+
									"</dl>";
						});
						$("#favorites").append(html);
					}
				}else{
					alert("获取收藏信息失败！");
				}
			}
		});
	}
}
/**
 *  取消收藏（课程）
 */
function delMyFavorites(id){
	alert(id);
	$.ajax({
		type:"post",
		url:"http://127.0.0.1:8080/inxweb/userInfo/delfavorites",
		data:{"ids":id},
		traditional: true,
		//async:true
		success: function(data){
			if(data.success){
				alert("取消收藏成功");
				window.location.reload();
			}else{
				alert("取消收藏失败！");
			}
		}
	});
}

/**
 * 把毫秒转换为("yyyy-MM-dd hh:mm:ss")格式
 */
function getMyDate(str){
	var oDate = new Date(str),
	oYear = oDate.getFullYear(),
	oMonth = oDate.getMonth()+1,
	oDay = oDate.getDate(),
	oHour = oDate.getHours(),
	oMin = oDate.getMinutes(),
	oSen = oDate.getSeconds(),
	oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen);//最后拼接时间
	return oTime;
};
//补0操作
function getzf(num){
	if(parseInt(num) < 10){
		num = '0'+num;
	}
	return num;
}
/**
 *  取消全部收藏
 */
function delMyFavoritesAll(){
	var ids = new Array();
	if( $('input[type=checkbox]:checked').length==0){
		alert("请选择要取消收藏的课程");
	}else{
		$.each($('input:checkbox:checked'),function(){
		 	//ids += $(this).val()+",";
		 	ids.push($(this).val());
        });
         /*ids = ids.substring(0,ids.length-1);*/
         delMyFavorites(ids);
	}
}
/**
 * 	选择个人头像
 */
$(function() {
	
	$("#pic_Img").change(function() {
		var $file = $(this);
		var fileObj = $file[0];
		var windowURL = window.URL || window.webkitURL;
		var dataURL;
		var $img = $("#preview");

		if(fileObj && fileObj.files && fileObj.files[0]) {
			dataURL = windowURL.createObjectURL(fileObj.files[0]);
			$.each($(".jcrop-preview"), function(i, d) {
				$(d).attr("src", dataURL)
			});
			$img.attr('src', dataURL);
		} else {
			dataURL = $file.val();
			var imgObj = document.getElementById("preview");
			// 两个坑:
			// 1、在设置filter属性时，元素必须已经存在在DOM树中，动态创建的Node，也需要在设置属性前加入到DOM中，先设置属性在加入，无效；
			// 2、src属性需要像下面的方式添加，上面的两种方式添加，无效；
			imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
			imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;

		}
	});
});
/**
 * 	修改用户头像
 */
function updateImg(){
	var user_id =$("#user_id").val();
	$("#uid").val(user_id);
	var myfrom=new FormData($("#myform")[0]);
		myfrom.append('file', $("#pic_Img")[0].files[0]);
	$.ajax({
		type:"post",
		url:"http://localhost:9095/user/update/img",
		data:myfrom,
		contentType: false, //这个一定要写
		processData: false, //这个也一定要写，不然会报错
		dataType: 'json', //返回类型，有json，text，HTML。这里并没有jsonp格式，所以别妄想能用jsonp做跨域了。
		async:true,
		headers:{
			"token":localStorage.getItem("token")
		},
		success:function(data){
//			if (data.success) {
//				if (data.token!=null && data.token!="") {
//					$.cookie("token",data.token,{expires:7,path:'/'});
//				}
//			}
			 parent.parent.location.reload(); 
			console.log(data);
		}
	});
}
